1789E - Serval and Music Game - CodeForces Solution


brute force math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include <stdio.h>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define clr(x) vector<int>().swap(x);
#define all(x) x.begin(), x.end()
#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define popf pop_front
#define ll long long
#define ld long double
#define ull unsigned long long
#define random mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F first
#define S second
const ll INF=1e18;
const ll MN=-1e9;
const ll MX=1e7+9;
const ll MXX=1e9;
const ll SQ=4e2;
const ll MOD=998244353;
const ll PP=1e6+3;
const ld PI=3.141592653589793;
const ld eps=1e-6;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

int n, m, k, cur, ans, f[MX];

int mul(int x, int y) {
    return 1ll*x*y % MOD;
}

int add(int x, int y) {
    x += y;
    if (x >= MOD) {
        x -= MOD;
    }
    return x;
}

int get(int x) {
    int res = n;
    for (int y=1; y<x && x*(y-1)+y<=m; y++) {
        int l = x*(y-1)+y;
        int r = x*y-1;
        r = min(r, m);
        res -= f[r] - f[l-1];
    }
    return res;
}

int go(int x) {
    int res = 0;
    for (int y=x; y<=m; y+=x) {
        res += f[y] - f[y-1];
    }
    return res;
}

void init() {
    ans = 0;
    for (int i=1; i<=m; i++) {
        f[i] = 0;
    }
}

int main() {
    FAST;
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i=1; i<=n; i++) {
            cin >> m;
            f[m]++;
        }
        for (int x=1; x<=m; x++) {
            f[x] += f[x-1];
        }
        for (int x=1; x<=m; x++) {
            k = m/x;
            if (m % x) {
                if (k < m/(x-1)) {
                    cur = get(k);
                }
            }
            else {
                cur = go(k);
            }
            ans = add(ans, mul(x, cur));
        }
        cout << ans << endl;
        init();
    }
}

/*



*/


Comments

Submit
0 Comments
More Questions

766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX